home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11583 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: news.mindspring.com!usenet
  2. From: rudd@mindspring.com (Justin Rudd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ newbie asks for help
  5. Date: Fri, 15 Mar 1996 03:49:21 GMT
  6. Organization: MindSpring Enterprises
  7. Message-ID: <4iapek$med@B1FF.mindspring.com>
  8. References: <31488AC8.1557@cco.caltech.edu>
  9. Reply-To: rudd@mindspring.com
  10. NNTP-Posting-Host: rudd.mindspring.com
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. This is a very common problem with 4.0 console apps.  The problem is
  14. you aren't filling the internal buffer with enough characters.  What
  15. this basically means is...after it does the cout << "starting\a\n";
  16. This doesn't fill the buffer enough to be dumped.  Its screwy but easy
  17. to fix...just add this line right after any cout or ostream operation.
  18.  
  19. cout.flush();  
  20.  
  21. This will flush everything in the buffer out to the screen.  The
  22. reason it has a buffer is because in 95 and NT it is not true DOS but
  23. a DOS session so they allocate a buffer for text and wait till its
  24. filled before dumping it.  Now don't think this is just because it is
  25. windows.  I've had this problem before with DOS and OS/2 :-)
  26.  
  27. >#include <iostream.h> 
  28. >#include <time.h> // describes clock() function, clock_t type 
  29. >int main(void) 
  30. >{
  31. >    cout << "Enter the delay time, in seconds: "; 
  32. >    float secs; cin >> secs;
  33. >        clock_t delay = secs * CLOCKS_PER_SEC;  
  34. >                                  // convert to clock ticks 
  35. >    cout << "starting\a\n";
  36.     cout.flush();
  37.     ^^^^^^^^^^
  38. >        clock_t start = clock();
  39. >        while (clock() - start < delay )       
  40. >                                     // wait until time elapses
  41. >                ; 
  42. >    cout << "done \a\n"; 
  43.     cout.flush();
  44.     ^^^^^^^^^
  45. >    return 0;
  46. >}
  47.  
  48. Same as the above...just insert cout.flush() after each line.
  49.  
  50. >Program #2
  51. >// textin3.cpp -- reading chars to end of file
  52. >#include <iostream.h>
  53. >int main(void)
  54. >{
  55. >    char ch;
  56. >    int count = 0;
  57.  
  58. >    while (cin.get(ch))      // cin.get(ch) is 0 on EOF
  59. >    {
  60. >        cout << ch;
  61. >        count++;
  62. >    }
  63. >    cout << count << " characters read\n";
  64. >    return 0;
  65. >}
  66.  
  67. Hope this helps :-)
  68.  
  69. Justin
  70.  
  71. P.S.( I say again....I have had this problem on DOS and OS/2 )
  72.  
  73.